package 晚间作业0316.Doc11_课后习题;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.zip.ZipEntry;

/**
 * 【最难】设计一个方法，将一个数组中的元素随机排列
 */
public class Doc11_05 {

    public static void main(String[] args) {
        Random random = new Random();
        List<Integer> list = Arrays.asList(1, 2, 3, 4);
        shuffle(list, random);
        System.out.println(list);
    }

    public static void shuffle(List<?> list, Random random) {
        if (list == null) { return; }
        int size = list.size();
        for (int i = size; i > 1; i--) {
            Collections.swap(list, i - 1, random.nextInt(i));
        }
    }
}
